package com.travelingsalesmen.post.gui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;

public class InvoicePanel extends JPanel {
    
    private static final String TEXT_FORMAT = "%-20s                           %8.2f                        %8d                     %8.2f                                 %8.2f";
    private List<String> lines;
    private double total;

    public InvoicePanel() {
        lines = new ArrayList<String>();
        initComponents();
    }
    
    public void addItem(String description, double unitPrice, int quanitiy, double subTotal, double runningSubtotal) {
        lines.add(String.format(TEXT_FORMAT, description, unitPrice, quanitiy, subTotal, runningSubtotal));
        total += subTotal;
        updateDisplay();
        
    }
    
    public void reset() {
        lines = new ArrayList<String>();
        total = 0;
        updateDisplay();
    }
    
    private void updateDisplay() {
        StringBuilder sb = new StringBuilder();
        for (String line : lines) {
            sb.append(line);
            sb.append('\n');
        }
        txtItemList.setText(sb.toString());
        Double subTotal = this.total;
        txtPrice.setText(String.format("$%.2f", subTotal));
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlInvoice = new javax.swing.JPanel();
        lblItem = new javax.swing.JLabel();
        lblUnitPrice = new javax.swing.JLabel();
        lblQuantity = new javax.swing.JLabel();
        lblSubTotal = new javax.swing.JLabel();
        lblRunningTotal = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        scpScrollPane = new javax.swing.JScrollPane();
        txtItemList = new javax.swing.JTextArea();
        lblCurrentTotal = new javax.swing.JLabel();

        pnlInvoice.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Invoice", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        lblItem.setText("ITEM");

        lblUnitPrice.setText("UNIT PRICE");

        lblQuantity.setText("QUANTITY");

        lblSubTotal.setText("SUB TOTAL");

        lblRunningTotal.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lblRunningTotal.setText("TOTAL");

        txtPrice.setEditable(false);
        txtPrice.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtPrice.setText("$0.00");
        txtPrice.setName(""); // NOI18N

        txtItemList.setColumns(20);
        txtItemList.setEditable(false);
        txtItemList.setRows(5);
        scpScrollPane.setViewportView(txtItemList);

        lblCurrentTotal.setText("CURRENT TOTAL");

        javax.swing.GroupLayout pnlInvoiceLayout = new javax.swing.GroupLayout(pnlInvoice);
        pnlInvoice.setLayout(pnlInvoiceLayout);
        pnlInvoiceLayout.setHorizontalGroup(
            pnlInvoiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlInvoiceLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlInvoiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(scpScrollPane)
                    .addGroup(pnlInvoiceLayout.createSequentialGroup()
                        .addGap(388, 388, 388)
                        .addComponent(lblRunningTotal)
                        .addGap(18, 18, 18)
                        .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlInvoiceLayout.createSequentialGroup()
                        .addComponent(lblItem)
                        .addGap(163, 163, 163)
                        .addComponent(lblUnitPrice)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblQuantity)
                        .addGap(39, 39, 39)
                        .addComponent(lblSubTotal)
                        .addGap(62, 62, 62)
                        .addComponent(lblCurrentTotal)))
                .addGap(21, 21, 21))
        );
        pnlInvoiceLayout.setVerticalGroup(
            pnlInvoiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlInvoiceLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlInvoiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblItem)
                    .addGroup(pnlInvoiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lblUnitPrice)
                        .addComponent(lblSubTotal)
                        .addComponent(lblCurrentTotal)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scpScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlInvoiceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblRunningTotal))
                .addGap(15, 15, 15))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlInvoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlInvoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel lblCurrentTotal;
    private javax.swing.JLabel lblItem;
    private javax.swing.JLabel lblQuantity;
    private javax.swing.JLabel lblRunningTotal;
    private javax.swing.JLabel lblSubTotal;
    private javax.swing.JLabel lblUnitPrice;
    private javax.swing.JPanel pnlInvoice;
    private javax.swing.JScrollPane scpScrollPane;
    private javax.swing.JTextArea txtItemList;
    private javax.swing.JTextField txtPrice;
    // End of variables declaration//GEN-END:variables

}
